// pages/index/choose/index.js
import CategoryApi from '../../../api/category'
import AchieveApi from '../../../api/achieve'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    option: [{
      text: '全部类型',
      value: ''
    }],
    params: {
      page: 1,
      size: 5,
      indexName: '',
      status: null,
      number: wx.getStorageSync('user').number,
      // array: ['美国', '中国', '巴西', '日本'],
    }, // 搜索和筛选的参数
    type: '',
    pagination: false,
    loading: false, // 是否显示正在加载字样
    loaded: false, // 是否显示已加载完成字样
    statusList: [{
        text: '全部状态',
        value: null
      }, {
        text: '未通过',
        value: -1
      },
      {
        text: '审核中',
        value: 0
      },
      {
        text: '已通过',
        value: 1
      }
    ],
    achieveList:[],
    status: null,
    date: '',
    // 显示日历
    showCalendar: false,
    minDate: new Date(2018, 0, 1).getTime(),
    maxDate: new Date().getTime(),
  },
    selectOwnAchieveList(){
    wx.showLoading({
      title: "加载中..."
    })
    var that = this
    // this.setData({
    //   ['params.page']: 1
    // })
    AchieveApi.getOwnAchieveList(this.data.params).then(res => {
      if (res.data.code === 200) {
        that.setData({
          achieveList: that.data.achieveList.concat(res.data.data.records)
        })
        if (res.data.data.pages <= that.data.params.page) {
          that.setData({
            loaded: true
          })
        }
      } else {
        wx.showToast({
          title: res.data.data,
          icon: 'none'
        })
      }
    })
      wx.hideLoading()
  },
  changeType({detail}){

    this.data.achieveList = []
    this.setData({
      ['params.indexName']: detail,
      ['params.page']: 1,
    })
    this.selectOwnAchieveList()
  },
  changeStatus({detail}) {
    this.data.achieveList = []
    this.setData({
      ['params.status']: detail,
      ['params.page']: 1,
    })
    this.selectOwnAchieveList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    CategoryApi.getCategoryList(this.data.pagination).then(data => {
      that.setData({
        categoryList: data.data.data.records
      })
      this.data.categoryList.forEach(value => {
        var newText = value.categoryName
        var newEname = value.ename
        var newData = {
          text: newText,
          value: newEname
        }
        that.setData({
          option: that.data.option.concat(newData)
        })
        // this.data.option.push({
        //   text: newText,
        //   value: newEname
        // })
      })
    });
    this.selectOwnAchieveList()
  },
  onReachBottom() {
    if (!this.data.loaded) {
      this.setData({
        loading: true,
        ['params.page']: this.data.params.page + 1
      })
      this.selectOwnAchieveList()
      this.setData({
        loading: false
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.setData({
      loading: false,
      loaded: false,
      ['params.page']: 1,
      ['params.indexName']: '',
      ['params.key']: '',
      ['params.status']: null,
      achieveList: [],
      index: 0
    })
    this.selectOwnAchieveList()
    setTimeout(function () {
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000)
  },
  onDisplay() {
    this.setData({
      showCalendar: true
    })
  },
  onClose() {
    this.setData({
      showCalendar: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    let year
    let month
    let day
    if (date.getMonth() + 1 < 10) {
      month = `0${date.getMonth() + 1}`
    } else {
      month = `${date.getMonth() + 1}`
    }
    if (date.getDate() < 10) {
      day = `0${date.getDate()}`
    } else {
      day = `${date.getDate()}`
    }
    year = `${date.getFullYear()}`
    // return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    return `${year}-${month}-${day}`
  },
  onConfirm(event) {
    this.data.achieveList = []
    const [start, end] = event.detail;
    this.setData({
      showCalendar: false,
      // date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      ['params.startDate']: `${this.formatDate(start)}`,
      ['params.endDate']: `${this.formatDate(end)}`,
      ['params.page']: 1,
    });
    this.selectOwnAchieveList()
  },
})
